package se.webbzon.boltzmann.sound;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayDeque;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

import se.webbzon.boltzmann.loader.Loader;
import se.webbzon.boltzmann.loader.LoaderException;
import se.webbzon.boltzmann.loader.LoaderUtilityPointer;
import se.webbzon.oschi01.sound.FiniteSoundSource;
import se.webbzon.oschi01.sound.MP3Source;
import se.webbzon.oschi01.sound.MemoryWavSource;
import se.webbzon.oschi01.sound.URLWavSource;

public class SoundLoaderMap implements Loader {
	
	// Whatever the loader should preload sounds or not
	private static boolean preloadSounds = false;
	
	// A collection of resource names related to this loader
	private final static Collection<String> resourceNames = new ArrayDeque<String>();

	static {
		resourceNames.add("sound");
		resourceNames.add("music");
	}

	// A map containing all loaded sounds
	private final Map<String,FiniteSoundSource> content;

	/*============================================================
	Constructors
	============================================================*/
	
	public SoundLoaderMap() {
		content = new HashMap<String,FiniteSoundSource>();
	}
	
	/*============================================================
	Public Methods
	============================================================*/
	
	/** Sets whatever the loader should preload sounds or not. **/
	public static final void setPreloadSounds(boolean preloadSounds) {
		SoundLoaderMap.preloadSounds = preloadSounds;
	}
	
	/** Returns whatever the loader should preload sounds or not. **/
	public static final boolean isPreloadingSounds() {
		return preloadSounds;
	}

	@Override public String getCategory() {
		return "sounds";
	}

	@Override public Collection<String> getSupportedResources() {
		return resourceNames;
	}

	@Override public void load(	LoaderUtilityPointer pointer, String resourceName,
								Map<String, String> attributeMap) throws LoaderException {
		String name = attributeMap.get("name");
		String filename = attributeMap.get("file");
		
		if (resourceName.equals("sound")) try {
			final URL url = new URL(pointer.getXMLURL(),filename);
			final FiniteSoundSource source;
			if (preloadSounds)
				source = MemoryWavSource.createSource(url); // Preload
			else
				source = URLWavSource.createSource(url); // By reference
			content.put(name, source);
		} catch (IOException e) {
			throw new LoaderException(e);
		} else if (resourceName.equals("music")) try {
			content.put(name, new MP3Source(new URL(pointer.getXMLURL(),filename)));
		} catch (MalformedURLException e) {
			throw new LoaderException(e);
		}
	}
	
	/** Returns the sound map associated with this instance. **/
	public Map<String,FiniteSoundSource> getMap() {
		return Collections.unmodifiableMap(content);
	}

}
